/*
 * Copyright (C) 2010 恒康信息科技有限公司
 * 版权所有
 */
package com.jjmmbb.dolist.ex;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.jjmmbb.anydolist.R;
import com.jjmmbb.anydolist.Util;
import com.jjmmbb.db.db.ItemManager;
import com.jjmmbb.db.pojo.Enums;
import com.jjmmbb.db.pojo.Item;

public class RepeatSetDialog extends Dialog implements
		android.view.View.OnClickListener, OnCheckedChangeListener {
	Context context;
	CheckBox onceView, dailyView, w1View, w2View, w3View, w4View, w5View,
			w6View, w7View;
	Button okView;

	Item item;
	ItemManager im;

	public RepeatSetDialog(Context context, Item item) {
		super(context);
		this.context = context;
		this.item = item;
		this.im = new ItemManager(context);
	}

	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.PROGRESS_VISIBILITY_ON);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.repeat_set);

		onceView = (CheckBox) findViewById(R.id.re_once);
		dailyView = (CheckBox) findViewById(R.id.re_everyday);
		w1View = (CheckBox) findViewById(R.id.re_w1);
		w2View = (CheckBox) findViewById(R.id.re_w2);
		w3View = (CheckBox) findViewById(R.id.re_w3);
		w4View = (CheckBox) findViewById(R.id.re_w4);
		w5View = (CheckBox) findViewById(R.id.re_w5);
		w6View = (CheckBox) findViewById(R.id.re_w6);
		w7View = (CheckBox) findViewById(R.id.re_w7);
		okView = (Button) findViewById(R.id.re_ok);

		w1View.setOnCheckedChangeListener(this);
		w2View.setOnCheckedChangeListener(this);
		w3View.setOnCheckedChangeListener(this);
		w4View.setOnCheckedChangeListener(this);
		w5View.setOnCheckedChangeListener(this);
		w6View.setOnCheckedChangeListener(this);
		w7View.setOnCheckedChangeListener(this);
		onceView.setOnCheckedChangeListener(this);
		dailyView.setOnCheckedChangeListener(this);
		okView.setOnClickListener(this);

		// LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数
		// if (null != p) {
		// p.width = Util.dip2px(context, 200);
		// }

		ini();
	}

	void ini() {
		if (null == item) {
			this.dismiss();
			return;
		}
		if (item.typeAlarm == Enums.Alarm_Once) {
			onceView.setChecked(true);
		} else if (item.typeAlarm == Enums.Alarm_Daily) {
			dailyView.setChecked(true);
		} else {
			ArrayList<Integer> list = Util.MarkDecodeByBinary(item.typeAlarm);
			boolean[] checkeds = new boolean[] { false, false, false, false,
					false, false, false, false, };
			for (int i : list) {
				checkeds[i] = true;
			}
			w1View.setChecked(checkeds[1]);
			w2View.setChecked(checkeds[2]);
			w3View.setChecked(checkeds[3]);
			w4View.setChecked(checkeds[4]);
			w5View.setChecked(checkeds[5]);
			w6View.setChecked(checkeds[6]);
			w7View.setChecked(checkeds[7]);
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton v, boolean b) {
		switch (v.getId()) {
		case R.id.re_once:
			onOnce(b);
			break;
		case R.id.re_everyday:
			onEveryDay(b);
			break;
		default:
			// 如果不是“一次”，“每天”，就是选中了其他，则将“一次”，“每天”设置为没有选中
			if (b) {
				onceView.setChecked(false);
				dailyView.setChecked(false);
			}
			break;
		}
	}

	private void onOnce(boolean b) {
		if (b) {
			dailyView.setChecked(false);
			w1View.setChecked(false);
			w2View.setChecked(false);
			w3View.setChecked(false);
			w4View.setChecked(false);
			w5View.setChecked(false);
			w6View.setChecked(false);
			w7View.setChecked(false);
		}
	}

	private void onEveryDay(boolean b) {
		if (b) {
			onceView.setChecked(false);
			w1View.setChecked(false);
			w2View.setChecked(false);
			w3View.setChecked(false);
			w4View.setChecked(false);
			w5View.setChecked(false);
			w6View.setChecked(false);
			w7View.setChecked(false);
		}
	}

	private void onOK() {
		ArrayList<Integer> list = new ArrayList<Integer>();

		if (onceView.isChecked()) {
			item.typeAlarm = Enums.Alarm_Once; // 一次
			item.update(im);
			this.dismiss();
			return;
		} else if (dailyView.isChecked()) {
			item.typeAlarm = Enums.Alarm_Daily; // 循环提醒
			item.update(im);
			this.dismiss();
			return;
		} else {
			if (w1View.isChecked()) {
				list.add(1);
			}
			if (w2View.isChecked()) {
				list.add(2);
			}
			if (w3View.isChecked()) {
				list.add(3);
			}
			if (w4View.isChecked()) {
				list.add(4);
			}
			if (w5View.isChecked()) {
				list.add(5);
			}
			if (w6View.isChecked()) {
				list.add(6);
			}
			if (w7View.isChecked()) {
				list.add(7);
			}
			item.typeAlarm = Util.MarkCodeByBinary(list);
			item.update(im);
			this.dismiss();
			return;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.re_ok:
			onOK();
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 按下键盘上返回按钮
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.dismiss();
		}
		return true;
	}

}
